home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cgazv5n5.arc / EXPRESS.Y < prev    next >
Text File  |  1991-09-23  |  3KB  |  138 lines

  1. %term  ID       /*  a string of lower-case characters   */
  2. %term  NUM      /*  a number                            */
  3.  
  4.  
  5.  
  6. %left  PLUS     /*  +                                   */
  7. %left  TIMES    /*  *                                   */
  8. %left  LP RP    /*  (  )                                */
  9.  
  10.  
  11.  
  12. %{
  13. #include <stdio.h>
  14. #include <ctype.h>
  15. #include <malloc.h>
  16.  
  17. extern char   *yytext;      /* In yylex(), holds lexeme        */
  18. extern char   *new_name();  /* declared at bottom of this file */
  19.  
  20. typedef char       *stype;  /* Type the value stack as char*   */
  21.  
  22. #define YYSTYPE    stype
  23.  
  24. %}
  25.  
  26. %%
  27.  
  28. /* A small expression grammar that recognizes numbers,names,
  29.  * addition (+), multiplication (*), and parentheses. Expressions
  30.  * associate left to right unless parentheses force it to go
  31.  * otherwise. + is lower precedence than *.  Note that an
  32.  * underscore is appended to identifiers so that they won't be
  33.  * confused with temporary variables.
  34.  */
  35.  
  36.  s  : e ;
  37.  
  38.  e  : e PLUS e  {printf("%s += %s;\n", $1, $3); free_name($3 ); }
  39.     | e TIMES e {printf("%s *= %s;\n", $1, $3); free_name($3 ); }
  40.     | LP e RP   {$$ = $2;                                       }
  41.     | NUM       {printf("%s = %s;\n",  $$ = new_name(),yytext );}
  42.     | ID        {printf("%s = _%s;\n", $$ = new_name(),yytext );}
  43.     ;
  44.  
  45. %%
  46.  
  47. /*-------------------------------------------------------------*/
  48. #ifdef OCCS
  49. char    *yypstk( ptr )
  50. char    **ptr;
  51. {
  52.     /* Used only by occs, (not by UNIX yacc), yypstk()is used
  53.      * by the debugging version of the parser to print the value
  54.      * stack. It is passed a pointer to a value-stack item and
  55.      * should return a string representing that item. In this
  56.      * case, all it has to do is dereference one level of
  57.      * indirection.
  58.      */
  59.  
  60.      return *ptr ? *ptr : "<empty>" ;
  61.  
  62. }
  63. #endif
  64.  
  65. /*-------------------------------------------------------------*/
  66.  
  67. char *Names[] = {"t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"};
  68. char **Namep  = Names;
  69.  
  70. char    *new_name()
  71. {
  72.  
  73.      /* Simplistic temporary-variable management. Return a
  74.       * temporary-variable name by popping one off the name stack.
  75.       */
  76.  
  77.      if( Namep >= &Names[ sizeof(Names)/sizeof(*Names) ] )
  78.      {
  79.         yyerror("Expression too complex\n");
  80.         exit( 1 );
  81.      }
  82.  
  83.      return( *Namep++ );
  84.  
  85. }
  86.  
  87. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  88.  
  89. free_name(s)
  90. char    *s;
  91. {               /* Free up a previously allocated name */
  92.     *--Namep = s;
  93. }
  94.  
  95. /*-------------------------------------------------------------*/
  96.  
  97. yy_init_occs()
  98. {
  99.     /* Generate declarations for the temporary variables
  100.  
  101.     printf("int t0, t1, t2, t3;\n");
  102.     printf("int t4, t5, t6, t7;\n");
  103. }
  104.  
  105. /*-------------------------------------------------------------*/
  106.  
  107. main( argc, argv )
  108.  
  109. int     argc;
  110. char    **argv;
  111. {
  112.     yy_get_args( argc, argv );
  113.  
  114.     if( argc < 2 )
  115.     {
  116.         fprintf( stderr, "Need file name\n");
  117.         exit(1);
  118.     }
  119.  
  120. #ifdef OCCS
  121.     else if( ii_newfile(argv[1]) << 0 )
  122. #else                           /* UNIX yacc */
  123.     else if( !freopen( argv[1], "r", stdin ))
  124. #endif
  125.  
  126.     {
  127.         fprintf( stderr, "Can't open %s\n", argv[1] );
  128.         exit(2);
  129.     }
  130.  
  131. #ifndef OCCS
  132.     yy_init_occs();
  133. #endif
  134.     yyparse();
  135.     exit( 0 );
  136. }
  137.  
  138.